﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Microsoft.Xna.Framework.Content;
using DARE.Properties;

namespace DARE
{
    public class CSkinnedBasicEffect: CBasicEffect
    {
        #region fields


        #endregion
                
        #region properties

       // public Matrix[] Bones { get; set; }

        #endregion

        #region ctor

        /// <summary>
        /// Constructs the CBasicEffect class with a default effect file ("Shaders/IntensityEffect").
        /// If you want to use this ctor, you have to create this effect file at this path.
        /// You also have to implement a technique named "SpecularLight" which will be the default technique.
        /// If you don't want to, use the second ctor.
        /// </summary>
        /// <param name="content">The content manager you want to use for this effect</param>
        public CSkinnedBasicEffect()
        {
            Name = "Shaders/SkinnedBasicEffect";
#if WINDOWS_PHONE
            m_effect = new SkinnedEffect(DARE.CDare.Instance.GraphicsDevice);
#else
            m_effect = CResourceMgr.Load<Effect>(DARE.Properties.Resources.SkinnedBasicEffect, Name);
            m_techniquePrefix = "Skinned";
            Initialize(m_techniquePrefix + "VertexLightBasic");
#endif       
        }
        
        #endregion

        #region Method

        
        public override CBasicEffect ProcessEffect(CRender3D render, CEffect effect)
        {
            

#if WINDOWS_PHONE

            SkinnedEffect e = m_effect as SkinnedEffect;
            e.SetBoneTransforms(effect.Bones);
            e.WeightsPerVertex = 1;

            if (effect.UseSceneParameters)
            {
                e.PreferPerPixelLighting = render.EffectMgr.PerPixelLighteningEnabled;
            }
            else
            {
                e.PreferPerPixelLighting = effect.PerPixelLighteningEnabled;
            }
            e.View = effect.View;
            e.Projection = effect.Projection;
            e.World = effect.World;
            e.Texture = effect.ColorMap;
            e.Alpha = effect.Alpha;
            e.EmissiveColor = effect.EmissiveLightColor.ToVector3();
            e.SpecularPower = effect.Brighteness;

            if (effect.UseSceneParameters)
            {
                e.AmbientLightColor = render.EffectMgr.AmbientLightColor.ToVector3();
                e.DirectionalLight0.Direction = render.EffectMgr.DirectionalLight0Direction;
                e.DirectionalLight0.DiffuseColor = render.EffectMgr.DirectionalLight0DiffuseColor.ToVector3();
                e.DirectionalLight0.Enabled = render.EffectMgr.DirectionalLight0DiffuseIntensity > 0 ? true : false;
                e.DirectionalLight0.SpecularColor = render.EffectMgr.DirectionalLight0SpecularColor.ToVector3();

                e.DirectionalLight1.Direction = render.EffectMgr.DirectionalLight1Direction;
                e.DirectionalLight1.DiffuseColor = render.EffectMgr.DirectionalLight1DiffuseColor.ToVector3();
                e.DirectionalLight1.Enabled = render.EffectMgr.DirectionalLight1DiffuseIntensity > 0 ? true : false;
                e.DirectionalLight1.SpecularColor = render.EffectMgr.DirectionalLight1SpecularColor.ToVector3();

                e.DirectionalLight2.Direction = render.EffectMgr.DirectionalLight2Direction;
                e.DirectionalLight2.DiffuseColor = render.EffectMgr.DirectionalLight2DiffuseColor.ToVector3();
                e.DirectionalLight2.Enabled = render.EffectMgr.DirectionalLight2DiffuseIntensity > 0 ? true : false;
                e.DirectionalLight2.SpecularColor = render.EffectMgr.DirectionalLight2SpecularColor.ToVector3();

            }
            else
            {
                e.AmbientLightColor = effect.AmbientLightColor.ToVector3();
                e.DirectionalLight0.Direction = effect.DirectionalLight0Direction;
                e.DirectionalLight0.DiffuseColor = effect.DirectionalLight0DiffuseColor.ToVector3();
                e.DirectionalLight0.Enabled = effect.DirectionalLight0DiffuseIntensity > 0 ? true : false;
                e.DirectionalLight0.SpecularColor = effect.DirectionalLight0SpecularColor.ToVector3();

                e.DirectionalLight1.Direction = effect.DirectionalLight1Direction;
                e.DirectionalLight1.DiffuseColor = effect.DirectionalLight1DiffuseColor.ToVector3();
                e.DirectionalLight1.Enabled = effect.DirectionalLight1DiffuseIntensity > 0 ? true : false;
                e.DirectionalLight1.SpecularColor = effect.DirectionalLight1SpecularColor.ToVector3();

                e.DirectionalLight2.Direction = effect.DirectionalLight2Direction;
                e.DirectionalLight2.DiffuseColor = effect.DirectionalLight2DiffuseColor.ToVector3();
                e.DirectionalLight2.Enabled = effect.DirectionalLight2DiffuseIntensity > 0 ? true : false;
                e.DirectionalLight2.SpecularColor = effect.DirectionalLight2SpecularColor.ToVector3();
            }

            m_effect = e;
#else
            base.ProcessEffect(render, effect);
            m_effect.Parameters["Bones"].SetValue(effect.Bones);
#endif
            return this;
        }


        #endregion
    }
}
